import Router from "koa-router";
import {uploadAudio} from '../config/multer'
import send from 'koa-send'

const router = new Router({
    prefix: "/user" // 设置路由前缀
});

router.get("/", async (ctx, next) => {
    ctx.body = "user根路径";
})

router.get("/info", async (ctx, next) => {
    ctx.body = {
        name: "张三",
        age: 18,
        gender: "男"
    };
})

router.post("/login", async (ctx, next) => {
    console.log(ctx.request.body);
    const name = ctx.request.body.name;
    const password = ctx.request.body.password;
    ctx.send('登录成功', 200, {
        'login': '登录',
        name,
        password
    })
})

router.post("/upload", uploadAudio.single('music'), async (ctx, next) => {
    console.log('upload.request:', ctx.request.file);
    console.log('upload.request.body:', ctx.request.body);
    ctx.send('上传成功', 200, {
        file: ctx.request.file,
        body: ctx.request.body
    })
})

// 下载文件
router.get('/download/:name', async (ctx) => {
    ctx.append("Accept-Encoding", "identity");
    const name = ctx.params.name;
    const path = `static/music/${name}`;
    ctx.attachment(path);
    await send(ctx, path);
})

export default router;